home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte13 / setdibit.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  10KB  |  299 lines

  1.  
  2. #include <windows.h>  
  3. #include "SetDIBit.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define ALIGNLONG(i) ((i+3)/4*4)
  108. #define WIDTH        50
  109. #define HEIGHT       50
  110. #define COLORBITS    4
  111.  
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115. static HBITMAP            hBitmap;
  116. static LPBITMAPINFOHEADER lpbi;
  117. static BITMAPINFOHEADER   bi;
  118.  
  119.    switch( uMsg )
  120.    {
  121.       case WM_CREATE :
  122.                {
  123.                   HDC    hDC, hMemDC;
  124.                   LPTSTR lpstBitmap;
  125.  
  126.                   hDC = GetDC (hWnd) ;
  127.                   
  128.                   // Intialize BITMAPINFOHEADER data.
  129.                   //.................................
  130.                   bi.biSize     = sizeof (BITMAPINFOHEADER) ;
  131.                   bi.biWidth    = WIDTH;     
  132.                   bi.biHeight   = HEIGHT;
  133.                   bi.biPlanes   = 1;
  134.                   bi.biBitCount = COLORBITS;
  135.                   bi.biCompression = BI_RGB;
  136.                   bi.biSizeImage = ( ALIGNLONG( (WIDTH * COLORBITS)/8 ) 
  137.                                          * HEIGHT);
  138.                   bi.biXPelsPerMeter = 0;
  139.                   bi.biYPelsPerMeter = 0;
  140.                   bi.biClrUsed       = 0;
  141.                   bi.biClrImportant  = 0;
  142.  
  143.                   // Create uninitialized DIB bitmap.
  144.                   //.................................
  145.                   hBitmap = CreateDIBitmap( hDC, &bi, 0L, NULL, 
  146.                                             NULL, 0);
  147.  
  148.                   // Allocate memory for BITMAPINFO structure.
  149.                   //..........................................
  150.                   lpbi = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, 
  151.                                        sizeof(BITMAPINFOHEADER) +
  152.                                        16 * sizeof (RGBQUAD) + 
  153.                                        (ALIGNLONG((WIDTH * COLORBITS)/8)
  154.                                        * HEIGHT) );
  155.                   
  156.                   // Copy BITMAPINFOHEADER information.
  157.                   //...................................
  158.                   *lpbi = bi;
  159.                   
  160.                   // Initialize structure data with GetDIBits().
  161.                   //............................................
  162.                   GetDIBits( hDC, hBitmap, 0, 50, NULL, 
  163.                             (LPBITMAPINFO) lpbi, DIB_RGB_COLORS);
  164.                   
  165.                   // Create memory device context and select bitmap.
  166.                   //................................................
  167.                   hMemDC = CreateCompatibleDC( hDC );
  168.                   
  169.                   SelectObject( hMemDC, hBitmap );
  170.                   
  171.                   // Paint on memory device context.
  172.                   //................................
  173.                   SelectObject( hMemDC, GetStockObject(BLACK_BRUSH) );
  174.                   Rectangle (hMemDC, 0, 0, WIDTH, HEIGHT);
  175.  
  176.                   SelectObject( hMemDC, GetStockObject(WHITE_BRUSH) ) ;
  177.                   Ellipse( hMemDC, 0, 0, WIDTH, HEIGHT );
  178.                   MoveToEx( hMemDC, 0, 0, NULL );
  179.                   LineTo( hMemDC, WIDTH / 2, HEIGHT / 2 );
  180.                   LineTo( hMemDC, WIDTH, 0 );
  181.                   
  182.                   // Set pointer to bitmapÆs bit data.
  183.                   //..................................
  184.                   lpstBitmap = (LPSTR)lpbi + (WORD)sizeof(BITMAPINFOHEADER) +
  185.                                              (16 * sizeof(RGBQUAD));
  186.  
  187.                   GetDIBits( hDC, hBitmap, 0, HEIGHT, lpstBitmap,
  188.                              (LPBITMAPINFO) lpbi, DIB_RGB_COLORS);
  189.  
  190.                   DeleteDC( hMemDC );
  191.                   ReleaseDC( hWnd, hDC);
  192.                }
  193.                break;
  194.  
  195.       case WM_PAINT :
  196.                {
  197.                   PAINTSTRUCT ps;
  198.                   HDC         hMemDC;
  199.  
  200.                   BeginPaint( hWnd, &ps );
  201.                   hMemDC = CreateCompatibleDC( ps.hdc );
  202.  
  203.                   SelectObject( hMemDC, hBitmap );
  204.                   BitBlt( ps.hdc, 10, 10, WIDTH, HEIGHT, 
  205.                           hMemDC, 0, 0, SRCCOPY );
  206.  
  207.                   DeleteDC( hMemDC );
  208.                   EndPaint( hWnd, &ps );
  209.                }
  210.                break;
  211.  
  212.       case WM_COMMAND :
  213.               switch( LOWORD( wParam ) )
  214.               {
  215.                  case IDM_TEST :
  216.                         { 
  217.                            HDC    hDC;
  218.                            int    nBytes, i;
  219.                            LPTSTR lpstBitmap, lpstTemp;
  220.  
  221.                            hDC = GetDC( hWnd );
  222.  
  223.                            lpstBitmap = (LPSTR)lpbi + 
  224.                                         (WORD) sizeof(BITMAPINFOHEADER) +
  225.                                          (16 * sizeof (RGBQUAD));
  226.  
  227.                            nBytes = ALIGNLONG((WIDTH * COLORBITS)/8) 
  228.                                     * HEIGHT;
  229.  
  230.                            lpstTemp = lpstBitmap;
  231.                   
  232.                            // Change 0 bytes to 0x2C in bitmap data.
  233.                            //........................................         
  234.                            for( i = 0; i < nBytes; i++ )
  235.                            {
  236.                               if ( 0 == *lpstTemp )
  237.                                  *lpstTemp = 0x2C;
  238.  
  239.                               lpstTemp++;
  240.                            }
  241.  
  242.                            // Make the changes to the bitmap.
  243.                            //................................
  244.                            SetDIBits( hDC, hBitmap, 0, HEIGHT, lpstBitmap,
  245.                                       (LPBITMAPINFO) lpbi, DIB_RGB_COLORS );
  246.  
  247.                            ReleaseDC( hWnd, hDC );
  248.  
  249.                            InvalidateRect( hWnd, NULL, TRUE );
  250.                            UpdateWindow( hWnd );
  251.                         }
  252.                         break;
  253.  
  254.                  case IDM_ABOUT :
  255.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  256.                         break;
  257.  
  258.                  case IDM_EXIT :
  259.                         DestroyWindow( hWnd );
  260.                         break;
  261.               }
  262.               break;
  263.       
  264.       case WM_DESTROY :
  265.               GlobalFree( lpbi );
  266.               PostQuitMessage(0);
  267.               break;
  268.  
  269.       default :
  270.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  271.    }
  272.  
  273.    return( 0L );
  274. }
  275.  
  276.  
  277. LRESULT CALLBACK About( HWND hDlg,           
  278.                         UINT message,        
  279.                         WPARAM wParam,       
  280.                         LPARAM lParam)
  281. {
  282.    switch (message) 
  283.    {
  284.        case WM_INITDIALOG: 
  285.                return (TRUE);
  286.  
  287.        case WM_COMMAND:                              
  288.                if (   LOWORD(wParam) == IDOK         
  289.                    || LOWORD(wParam) == IDCANCEL)    
  290.                {
  291.                        EndDialog(hDlg, TRUE);        
  292.                        return (TRUE);
  293.                }
  294.                break;
  295.    }
  296.  
  297.    return (FALSE); 
  298. }
  299.